package com.zlyx.easy.web.web.controller;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlyx.easy.core.annotations.Desc;
import com.zlyx.easy.swagger.annotations.SpringMapping;
import com.zlyx.easy.web.web.mybatis.AbstractMapper;
import com.zlyx.easy.web.web.service.AbstractService;

/**
 * @Auth 赵光
 * @Describle
 * @Date 2019年1月4日 下午8:59:49
 */
public class AbstractController<Service extends AbstractService<? extends AbstractMapper<T>, T>, T>
		extends BaseController {

	@Autowired
	protected Service baseService;

	@SpringMapping(notes = "新增", todo = { "新增" }, value = { "/insert" })
	public Object insert(T entity) {
		return this.baseService.save(entity);
	}

	@SpringMapping(notes = "批量新增", todo = { "批量新增" }, value = { "/insertBatch" })
	public Object insertBatch(List<T> entitys) {
		return this.baseService.saveBatch(entitys);
	}

	@SpringMapping(notes = "删除", todo = { "根据id删除" }, value = { "/deleteById" })
	public Object deleteById(Serializable id) {
		return this.baseService.getById(id);
	}

	@SpringMapping(notes = "批量删除", todo = { "根据id批量删除" }, value = { "/deleteBatchById" })
	public Object deleteBatchById(List<? extends Serializable> entitys) {
		return this.baseService.removeByIds(entitys);
	}

	@SpringMapping(notes = "更新", todo = { "更新" }, value = { "/updateById" })
	public Object updateById(T entity) {
		return this.baseService.updateById(entity);
	}

	@SpringMapping(notes = "批量更新", todo = { "根据id批量更新" }, value = { "/updateBatchById" })
	public Object updateBatchById(List<T> entitys) {
		return this.baseService.updateBatchById(entitys);
	}

	@SpringMapping(notes = "批量更新", todo = { "根据id批量更新" }, value = { "/page" })
	public Object page(int pageNum, int pageSize, T entity) {
		return doPage(pageNum, pageSize, entity);
	}

	@Desc("请根据需要自己实现")
	protected IPage<Map<String, Object>> doPage(int pageNum, int pageSize, T entity) {
		return this.baseService.pageMaps(new Page<>(pageNum, pageSize), new QueryWrapper<T>(entity));
	}
}
